package com.csweimei.rentmanagementtreasure.enterprise.viewModel;

import android.app.Application;
import android.view.View;
import android.widget.TimePicker;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.chad.library.BR;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.http.entity.EntHomeDailyBean;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class EntDayPaperVM extends TitleVM {


    public EntDayPaperVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("日报记录");
        setRightImg(R.mipmap.title_more);
    }

    public ObservableField<String> companyid = new ObservableField<>();
    public ObservableField<String> data = new ObservableField<>(ValidateUtils.getCurDate());

    @Override
    public void onResume() {
        super.onResume();
        getDailyData();
    }

    @Override
    protected void rightIconOnClick() {
        super.rightIconOnClick();
        TimePickerView pvTime = new TimePickerBuilder(mActivity, (date, v) -> {
            data.set(new SimpleDateFormat("yyyy-MM-dd").format(date));
        })
                .setType(new boolean[]{true, true, true, false, false, false})
                .setLabel("年", "月", "日", "", "", "")
                .build();
        pvTime.show();
    }




    public ObservableList<EntHomeDailyItemVM> observableList = new ObservableArrayList<>();
    public ItemBinding<EntHomeDailyItemVM> itemBinding = ItemBinding.of(BR.viewModel,R.layout.item_rb_ent);
    public ObservableField<Integer> showNoD = new ObservableField<>(View.VISIBLE);

    /**
     * 获取日报列表
     */
    private void getDailyData() {
        Map<String,String> map = new HashMap<>();
        map.put("companyid",companyid.get());
        map.put("data", data.get());
        model.companydaily(map)
                .compose(RxUtils.exceptionTransformer())
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<EntHomeDailyBean>() {
                    @Override
                    public void onNext(EntHomeDailyBean o) {
                        if (o.issucc){
                            observableList.clear();
                            if (o.getData().size()>0){
                                if (o.getData().get(0).getDaily().size() > 0){
                                    showNoD.set(View.GONE);
                                    for (EntHomeDailyBean.DataDTO bean : o.getData()) {
                                        EntHomeDailyItemVM entHomeDailyItemVM = new EntHomeDailyItemVM(titleVM);
                                        entHomeDailyItemVM.setData(bean);
                                        observableList.add(entHomeDailyItemVM);
                                    }
                                }else {
                                    showNoD.set(View.VISIBLE);
                                }
                            }else {
                                showNoD.set(View.VISIBLE);
                            }
                        }else {
                            showNoD.set(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        showNoD.set(View.VISIBLE);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
